/**
 * Created by Administrator on 2017/4/20.
 */


var Poker = require('../Poker/Poker');
var DDZ = require('../Poker/DDZ');
var PokerPlayer = require('../Poker/player').PokerPlayer;

var Const = require('./const');
var SubCode = require('../subCode');



exports.LXELFDDZPlayer = PokerPlayer.extend({
    _tiCard: 0,
    _isDiZhu: 0,
    _tiStat: 0,
    _lastCards: {},
    _jiaoStatus: -1,
    _curRate: 0,
    _ti: 0,
    _jiaoDiZhu: 0,
    _roundTi: 0,
    _partner: 0,
    _bomb: 0,
    _pass: true,
    init: function(uid, msg) {
        this._super(uid, msg);
        this._stat._roundCards = [];
        this._tiCard = -1;              //-1未选择，,0不踢，1踢了,
        this._tiStat = -1;              //-1可以踢，0不能踢可以跟踢，1已经踢
        this._curRate = 0;
        this._roundTi = Const.PlayerStatus.NONE;
        this._pass = true;
        this._isDiZhu = false;
        this._bomb = 0;
        this._jiaoDiZhu = -1;           //-1未选择，0不叫，123分数
        this._lastCards = {
            pass: -1
        };
        this._partner = 0;             //队友的下标0表示没队友
        this._jiaoStatus = Const.PlayerStatus.NONE;
    },
    isTiSelected: function(){
        return (this._tiCard >= 0);
    },
    isTiCard: function(){
        return (this._tiCard == 1);
    },
    isScoreSelected: function(){
        return (this._jiaoDiZhu >= 0);
    },
    haveBomb: function(){
        var cards= this._handCards.getCards();
        var costCards = DDZ.getCostCards(cards);
        var resp = false;
        ExObject.eachKeyNum(costCards, function(c, v){
            if(v.num >= 4){
                resp = true;
                return false;
            }
        });
        if(resp){
            return resp;
        }
        if(cards[Poker.SpecPoint.JOKER_B] == 1 && cards[Poker.SpecPoint.JOKER_S] == 1){
            resp = true;
        }
        return resp;
    },
    checkSpecBomb: function(cards) {
        if (DDZ.getCardsNum(cards) != 2) {
            return false;
        }
        var costCards = DDZ.getCostCards(cards);
        var resp = false;
        ExObject.eachKeyNum(costCards, function (c, v) {
            if (c < 100) {
                if (v.point == Const.specBomb.THREE && v.num >= 2) {
                    resp = true;
                }
                if (v.point ==  Const.specBomb.TWO && v.num >= 2) {
                    resp = true;
                }
            }
        });
        return resp;
    },
    checkPattern: function(pattern, cards){
        var resp = false;

        var player = this;
        var room = player.getRoom();

        if(pattern){
            switch (pattern){
                case Const.Pattern.SINGLE :
                    resp = DDZ.checkSingle(cards);
                    break;
                case Const.Pattern.RUN :
                    resp = DDZ.checkRun(cards);
                    break;
                case Const.Pattern.SPEC_BOMB :
                    if(room.getSubType() == 'LFDDZ') {
                        resp = player.checkSpecBomb(cards);
                    }
                    break;
                case Const.Pattern.PAIR :
                    resp = DDZ.checkPair(cards);
                    break;
                case Const.Pattern.PAIRS :
                    resp = DDZ.checkPairs(cards);
                    break;
                case Const.Pattern.TRIPLE :
                    resp = DDZ.checkTriple(cards);
                    break;
                case Const.Pattern.TRIPLE_ONE :
                    resp = DDZ.checkTripleOne(cards);
                    break;
                case Const.Pattern.RUN_TRIPLE :
                    if(room.getSubType() == 'DDZ'){
                        resp = DDZ.checkRunTriple(cards);
                    }
                    break;
                case Const.Pattern.AIRPLANE :
                    resp = DDZ.checkAirplane(cards);
                    break;

                case Const.Pattern.FOUR_TWO :
                    resp = DDZ.checkFourTwo(cards);
                    break;

                case Const.Pattern.BOMB :
                    resp = DDZ.checkBomb(cards);
                    break;
                case Const.Pattern.TRIPLE_PAIR :
                    if(room.getSubType() == 'DDZ'){
                        resp = DDZ.checkTripleTwo(cards);
                    }
                    break;
                case Const.Pattern.FOUR_TWO_PAIR :
                    if(room.getSubType() == 'DDZ'){
                        resp = DDZ.checkFourTwoPair(cards);
                    }
                    break;
                case Const.Pattern.AIRPLANE_TWO :
                    if(room.getSubType() == 'DDZ'){
                        resp = DDZ.checkAirplaneTwo(cards);
                    }
                    break;
                case Const.Pattern.KING_BOMB :
                    resp = DDZ.checkKingBomb(cards);
                    break;
            }
        }
        return resp;
    },

    playCards: function(cards){
        if(ExObject.isArray(cards)){
            cards = this.getObj(cards);
        }
        var player = this;
        ExObject.eachKeyNum(cards, function (c, n) {
            player._handCards.delCard(c);
        });
    },
    getObj: function(cards){
        var obj = {};
        for(var i = 0; i < cards.length; i++){
            ExObject.addNumber0(obj,cards[i],1)
        }
        return obj;
    },
    checkMaxTriple: function(cards){
        var num = DDZ.getCardsNum(cards);
        num /= 4;
        var costCards = DDZ.getCostCards(cards);
        var triples = {};
        ExObject.eachKeyNum(costCards, function(c, v){
            if(v.num == 3){
                triples[c] = 1;
            }else if(v.num == 4){
                triples[c] = 1;
            }
        });
        var resp = true;

        var maxCost = 0;
        ExObject.eachKeyNum(triples, function(c, v){
            if(v > 0){
                if(c > maxCost){
                    maxCost = c;
                }
            }
        });
        for(var i = maxCost; i > maxCost - num; i--){
            if(triples[i] != 1){
                resp = false;
                break;
            }
        }
        if(resp){
            return maxCost;
        }else{
            resp = true;
        }
        var minCost = maxCost;
        ExObject.eachKeyNum(triples, function(c, v){
            if(v > 0) {
                if (c < minCost) {
                    minCost = c;
                }
            }
        });
        for(var j = minCost; j < minCost + num; j++){
            if(triples[j] != 1){
                resp = false;
                break;
            }
        }
        if(resp){
            return minCost + num - 1;
        }
        return 0;
    },
    checkMaxTripleTwo: function(cards){
        var num = DDZ.getCardsNum(cards);
        num /= 5;
        var costCards = DDZ.getCostCards(cards);
        var triples = {};
        ExObject.eachKeyNum(costCards, function(c, v){
            if(v.num == 3){
                triples[c] = 1;
            }else if(v.num == 4){
                triples[c] = 1;
            }
        });
        var resp = true;

        var maxCost = 0;
        ExObject.eachKeyNum(triples, function(c, v){
            if(v > 0){
                if(c > maxCost){
                    maxCost = c;
                }
            }
        });
        for(var i = maxCost; i > maxCost - num; i--){
            if(triples[i] != 1){
                resp = false;
                break;
            }
        }
        if(resp){
            return maxCost;
        }else{
            resp = true;
        }
        var minCost = maxCost;
        ExObject.eachKeyNum(triples, function(c, v){
            if(v > 0) {
                if (c < minCost) {
                    minCost = c;
                }
            }
        });
        for(var j = minCost; j < minCost + num; j++){
            if(triples[j] != 1){
                resp = false;
                break;
            }
        }
        if(resp){
            return minCost + num - 1;
        }
        return 0;
    },
    getMaxCard: function(cards, pattern){
        var card = 0;
        if(pattern){
            card = DDZ.getMaxCost(cards);
            switch (pattern){
                case Const.Pattern.SINGLE :
                    break;
                case Const.Pattern.PAIR :
                    break;
                case Const.Pattern.RUN :
                    break;
                case Const.Pattern.PAIRS :
                    break;
                case Const.Pattern.TRIPLE :
                    break;
                case Const.Pattern.TRIPLE_ONE :
                case Const.Pattern.TRIPLE_PAIR :
                    var costCards = DDZ.getCostCards(cards);
                    ExObject.eachKeyNum(costCards, function(c, v){
                        if(v.num >= 3){
                            card = c;
                        }
                    });
                    break;
                case Const.Pattern.AIRPLANE :
                    card = this.checkMaxTriple(cards);
                    break;
                case Const.Pattern.AIRPLANE_TWO :
                    card = this.checkMaxTripleTwo(cards);
                    break;
                case Const.Pattern.FOUR_TWO :
                case Const.Pattern.FOUR_TWO_PAIR :
                    var costCards = DDZ.getCostCards(cards);
                    card = 0;
                    ExObject.eachKeyNum(costCards, function(c, v){
                        if(v.num == 4){
                            if(card < c){
                                card = c;
                            }
                        }
                    });
                    break;
                case Const.Pattern.SPEC_BOMB :
                    break;
                case Const.Pattern.BOMB :
                    break;
                case Const.Pattern.KING_BOMB :
                    break;
            }
        }
        return card;
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getInfo: function() {
        var room = this.getRoom();
        var info = this._super();

        //info.handCards = room._settlementStatus ? {} : this._handCards.getCards();

        info.score = this._stat.score;
        //info.roundCards = this._stat._roundCards;
        info.remainScore = this._stat.score;
        info.handCardsNum = this._handCards.getCardNum();

        return info;
    },

    getReconnectInfo: function(isSelf) {
        var info = this._super(isSelf);
        if(isSelf){
            info.handCards = this._cardObjToArray(this._handCards.getCards());
        }
        info.status = this._jiaoStatus;
        info.handCardsNum = this._handCards.getCardNum();
        info.pass = this._pass;
        info.tiStat = this._tiCard;
        info.playCards = this._lastCards;
        info.score = this._stat.score;
        info.curRate = this._curRate;
        //info.roundCards = this._stat._roundCards;
        return info;
    },

    getSettlementInfo: function() {
        var info = this._super();
        info.handCards = this._cardObjToArray(this._handCards.getCards());
        info.bomb = this._bomb;
        info.roundTi = this.isTiCard();
        info.score = this._stat.score;

        return info;
    },

    settlementPattern: function() {
        var room = this.getRoom();
        var player = this;
        var subConfig = room.getSubConfig();
    },

    settlement: function() {
        this._super();

    },
    getInitHandCardNeedNum: function() {
        var room = this.getRoom();
        if(room.getSubType() == 'LFDDZ'){
            return 16;
        }else{
            return 17;
        }
    },
    getInitHandCardPreCards: function() {
        this._super();
        var cards = [];
        if(this.getIndex() == 1){
            cards.push(113);
            cards.push(213);
            cards.push(313);
            cards.push(413);

            cards.push(112);
            cards.push(212);
            cards.push(312);
            cards.push(412);

            cards.push(103);
            cards.push(203);
            cards.push(303);
            cards.push(403);

            cards.push(101);
            cards.push(201);
            cards.push(301);
            cards.push(401);
        }else if(this.getIndex() == 2){
            cards.push(104);
            cards.push(204);
            cards.push(304);
            //cards.push(404);

            cards.push(105);
            cards.push(205);
            cards.push(305);
            //cards.push(405);

            cards.push(106);
            cards.push(206);
            cards.push(306);
            //cards.push(406);

            cards.push(107);
            cards.push(207);
            cards.push(307);
            //cards.push(407);
            cards.push(108);
            cards.push(208);
            cards.push(308);

        }else if(this.getIndex() == 3){
            cards.push(1);
            cards.push(2);
            cards.push(102);
            cards.push(302);

            cards.push(109);
            cards.push(209);
            cards.push(309);
            cards.push(409);

            cards.push(110);
            cards.push(210);
            cards.push(310);
            cards.push(410);

            cards.push(111);
            cards.push(211);
            cards.push(311);
            cards.push(411);
        }
        return [];
    },
    onStartNewRound: function() {
        this._super();
        this._curRate = 1;
        this._stat._roundCards = [];
        this._tiCard = -1;              //-1未选择，,0不踢，1踢了,
        this._tiStat = -1;              //-1可以踢，0不能踢可以跟踢，1已经踢
        this._pass = true;
        this._isDiZhu = false;
        this._bomb = 0;
        this._partner = 0;
        this._lastCards = {
            pass: -1
        };
        this._jiaoDiZhu = -1;           //-1未选择，0不叫，123分数

        this._roundTi = Const.PlayerStatus.NONE;

        this._jiaoStatus = Const.PlayerStatus.NONE;
    },


    /******************************************************************
     * 重载的接口
     ******************************************************************/
    _cardObjToArray: function (arrayObj) {
        var keys = Object.keys(arrayObj);
        var temp = [];
        keys.forEach(function (v) {
            if (arrayObj[v]) {
                temp.push(v);
            }
        });
        return temp;
    },
});